﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CustomControls;
using DataAccess;
using Model.EntityModel;

namespace Project
{
    /// <summary>
    /// Interaction logic for RegisterComplaintContent.xaml
    /// </summary>
    public partial class ModifyComplaintContent : UserControl
    {
        public long ComplaintId { get; set; }
        private bool wasError;
        public ModifyComplaintPart2 modComplaintpart2;

        public ModifyComplaintContent(long idComplaint)
        {
            InitializeComponent();
            try
            {
                this.Loaded += loaded;
                ComplaintId = idComplaint;
                cboxCompCountry.ItemsSource = GetCountryList();
                cbVictimCountry.ItemsSource = GetCountryList();
                cbAggressorCountry.ItemsSource = GetCountryList();
                SetFieldRestrictions();
                lblCaseNumber.Content = ComplaintId;
                SetComplainantInfo();
                SetAggressorInfo();
                SetVictimInfo();
                SetComplaintInfo();
                UIWindowManager.SetWindowTitle(this, "Brigada De Protección a la Familia - Modificar Denuncia");
            }
            catch (Exception)
            {
                wasError = true;
                Xceed.Wpf.Toolkit.MessageBox.Show(
                    "La denuncia que esta tratando de acceder no esta completada, solo tiene acción directa", "Error!",
                    MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void loaded(object sender, RoutedEventArgs e)
        {
            if (wasError)
                UIWindowManager.CleanWindow(this);
        }

        private void SetComplainantInfo()
        {
            Actor actorComplainant = PersonAndActorDataAccess.GetActorByTypeAndComplaintId("Denunciante", ComplaintId);
            var personComplainant = PersonAndActorDataAccess.GetPersonById(actorComplainant.IdPerson);
            tbCompName.Text = personComplainant.Name;
            tbCompLastName.Text = personComplainant.PaternalName;
            tbCompMiddleName.Text = personComplainant.MaternalName;
            CompBornDate.Text = personComplainant.BirthDate.Value.ToShortDateString();
            rbtnMaleComp.IsChecked = personComplainant.Gender[0] == 'M';
            rbtnFemaleComp.IsChecked = personComplainant.Gender[0] == 'F';
            tbCompCI.Text = personComplainant.CiPassport.ToString();
            tbCompPhone.Text = personComplainant.Phone;
            cbCompCivilStatus.SelectedValue = personComplainant.CivilStatus;
            tbCompAddress.Text = personComplainant.Address;
            cboxCompCountry.SelectedValue = personComplainant.Nationality;
            tbCompZone.Text = personComplainant.Zone;
            tbOcc.Text = personComplainant.Ocupation;
        }

        private void SetAggressorInfo()
        {
            Actor actorAggressor = PersonAndActorDataAccess.GetActorByTypeAndComplaintId("Agresor", ComplaintId);
            var personAggressor = PersonAndActorDataAccess.GetPersonById(actorAggressor.IdPerson);
            tbAggressorCi.Text = personAggressor.CiPassport.ToString();
            tbAggressorName.Text = personAggressor.Name;
            tbAggressorLastName.Text = personAggressor.PaternalName;
            tbAggressorMiddleName.Text = personAggressor.MaternalName;
            AggressorBornDate.Text = personAggressor.BirthDate.Value.ToShortDateString();
            rbtnMaleAggressor.IsChecked = personAggressor.Gender[0] == 'M';
            rbtnFemaleAggressor.IsChecked = personAggressor.Gender[0] == 'F';
            tbAggressorPhone.Text = personAggressor.Phone;
            cbAggressorCivilStatus.SelectedValue = personAggressor.CivilStatus;
            cbAggressorCountry.SelectedValue = personAggressor.Nationality;
            tbAggressorBornPlace.Text = personAggressor.BornCity;
            rbtnAggressorUrban.IsChecked = personAggressor.BornArea == "Urbana";
            rbtnAggressorRural.IsChecked = personAggressor.BornArea == "Rural";
            cbAggressorInstructionLevel.SelectedValue = personAggressor.LevelOfEducation;
            rbtnAggressorCondActBusy.IsChecked = (personAggressor.ActivityStatus ==
                                                  rbtnAggressorCondActBusy.Content.ToString());
            rbtnAggressorCondActFree.IsChecked = (personAggressor.ActivityStatus ==
                                                  rbtnAggressorCondActFree.Content.ToString());
            rbtnAggressorCondActInactive.IsChecked = (personAggressor.ActivityStatus ==
                                                  rbtnAggressorCondActInactive.Content.ToString());
            //temperancia de denuncia
        }

        private void SetVictimInfo()
        {
            Actor actorVictim = PersonAndActorDataAccess.GetActorByTypeAndComplaintId("Victima", ComplaintId);
            var personVictim = PersonAndActorDataAccess.GetPersonById(actorVictim.IdPerson);
            tbVictimName.Text = personVictim.Name;
            tbVictimLastName.Text = personVictim.PaternalName;
            tbVictimMiddleName.Text = personVictim.MaternalName;
            VictimBornDate.Text = personVictim.BirthDate.Value.ToShortDateString();
            rbtnMaleVictim.IsChecked = personVictim.Gender[0] == 'M';
            rbtnFemaleVictim.IsChecked = personVictim.Gender[0] == 'F';
            tbVictimCI.Text = personVictim.CiPassport.ToString();
            tbVictimPhone.Text = personVictim.Phone;
            cbVictimCivilStatus.SelectedValue = personVictim.CivilStatus;
            tbVictimBornPlace.Text = personVictim.BornCity;
            cbVictimCountry.SelectedValue = personVictim.Nationality;
            rbtnVictimUrban.IsChecked = personVictim.BornArea == "Urbana";
            rbtnVictimRural.IsChecked = personVictim.BornArea == "Rural";
            cbVictimInstructionLevel.SelectedValue = personVictim.LevelOfEducation;
            rbtnVictimCondActBusy.IsChecked = (personVictim.ActivityStatus ==
                                                  rbtnVictimCondActBusy.Content.ToString());
            rbtnVictimCondActFree.IsChecked = (personVictim.ActivityStatus ==
                                                  rbtnVictimCondActFree.Content.ToString());
            rbtnVictimCondActInactive.IsChecked = (personVictim.ActivityStatus ==
                                                  rbtnVictimCondActInactive.Content.ToString());
            //relacion con agresor de denuncia
        }

        private void SetComplaintInfo()
        {
            var complaint = ComplaintDataAccess.GetComplaintById(ComplaintId);
            lblNatureOfFact.Content = complaint.IncidentNature;
            rbtnPublicAmbit.IsChecked = complaint.IncidentPlace == "Ambito Publico";
            rbtnPrivateAmbit.IsChecked = complaint.IncidentPlace == "Ambito Privado";
            rbtnZoneCentral.IsChecked = complaint.IncidentZone == "Urbano Central";
            rbtnZonePeriferic.IsChecked = complaint.IncidentZone == "Urbano Periferica";
            rbtnZoneRural.IsChecked = complaint.IncidentZone == "Rural";
            lblRegistrationDate.Content = complaint.RegistrationDate.ToShortDateString();
            dateIncident.Text = complaint.IncidentDate.Value.ToShortDateString();
            cboxCompPartnership.SelectedValue= complaint.ComplainantVictimRelationship;
            cboxVictimPartnership.SelectedValue = complaint.VictimAgressorRelationship;
            rbtnAggressorTempDrugs.IsChecked = (complaint.ConductOfAgressor == rbtnAggressorTempDrugs.Content.ToString());
            rbtnAggressorTempDrunk.IsChecked = (complaint.ConductOfAgressor == rbtnAggressorTempDrunk.Content.ToString());
            rbtnAggressorTempSane.IsChecked = (complaint.ConductOfAgressor == rbtnAggressorTempSane.Content.ToString());
            SetViolenceCauses(complaint);
            SetViolenceForms(complaint);
            SetMultidisciplinaryTeam(complaint);
            SetCaseDerivations(complaint);
            if (complaint.CasesAttention == "Citacion Y Arresto")
            {
                chkArrest.IsChecked = true;
                chkMeeting.IsChecked = true;
            }
            if (complaint.CasesAttention == "Arresto")
            {
                chkArrest.IsChecked = true;
            }
            if (complaint.CasesAttention == "Citacion")
            {
                chkMeeting.IsChecked = true;
            }
        }

        private void SetViolenceCauses(Complaint complaint)
        {
            foreach (var violenceCause in complaint.ViolenceCause)
            {
                if (chkEconomics.Content.ToString() == violenceCause.Cause)
                    chkEconomics.IsChecked = true;
                if (chkFamiliar.Content.ToString() == violenceCause.Cause)
                    chkFamiliar.IsChecked = true;
                if (chkJealous.Content.ToString() == violenceCause.Cause)
                    chkJealous.IsChecked = true;
                if (chkInfidelity.Content.ToString() == violenceCause.Cause)
                    chkInfidelity.IsChecked = true;
                if (chkOthersNature.Content.ToString() == violenceCause.Cause)
                    chkOthersNature.IsChecked = true;
                if (chkDomainHabits.Content.ToString() == violenceCause.Cause)
                    chkDomainHabits.IsChecked = true;
            }
        }

        private void SetViolenceForms(Complaint complaint)
        {
            foreach (var violenceForm in complaint.ViolenceForm)
            {
                if (chkPhysics.Content.ToString() == violenceForm.Form)
                    chkPhysics.IsChecked = true;
                if (chkPsicologics.Content.ToString() == violenceForm.Form)
                    chkPsicologics.IsChecked = true;
                if (chkSexual.Content.ToString() == violenceForm.Form)
                    chkSexual.IsChecked = true;
                if (chkOthersViolenceForm.Content.ToString() == violenceForm.Form)
                    chkOthersViolenceForm.IsChecked = true;
            }
            var firstOrDefault = complaint.ViolenceForm.FirstOrDefault();
            if (firstOrDefault != null)
                tbDescription.Text = firstOrDefault.Description;
        }

        private void SetMultidisciplinaryTeam(Complaint complaint)
        {
            foreach (var multiTeam in complaint.MultidisciplinaryTeam)
            {
                if (chkMedic.Content.ToString() == multiTeam.Professional)
                    chkMedic.IsChecked = true;
                if (chkPsicology.Content.ToString() == multiTeam.Professional)
                    chkPsicology.IsChecked = true;
                if (chkSocialWorker.Content.ToString() == multiTeam.Professional)
                    chkSocialWorker.IsChecked = true;
                if (chkLegalAsessor.Content.ToString() == multiTeam.Professional)
                    chkLegalAsessor.IsChecked = true;
            }
        }

        private void SetCaseDerivations(Complaint complaint)
        {
            foreach (var caseDeriv in complaint.CaseDerivation)
            {
                if (chkDerivJudge.Content.ToString() == caseDeriv.Institution)
                    chkDerivJudge.IsChecked = true;
                if (chkDerivPublicMinistery.Content.ToString() == caseDeriv.Institution)
                    chkDerivPublicMinistery.IsChecked = true;
                if (chkDerivFELCC.Content.ToString() == caseDeriv.Institution)
                    chkDerivFELCC.IsChecked = true;
                if (chkDerivDefensory.Content.ToString() == caseDeriv.Institution)
                    chkDerivDefensory.IsChecked = true;
                if (chkDerivOther.Content.ToString() == caseDeriv.Institution)
                    chkDerivOther.IsChecked = true;
            }
        }


        private void SetFieldRestrictions()
        {
            //Date Restrictions
            CompBornDate.DisplayDateEnd = DateTime.Now;
            CompBornDate.DisplayDateStart = DateTime.Now.AddYears(-100);
            AggressorBornDate.DisplayDateEnd = DateTime.Now;
            AggressorBornDate.DisplayDateStart = DateTime.Now.AddYears(-100);
            VictimBornDate.DisplayDateEnd = DateTime.Now;
            VictimBornDate.DisplayDateStart = DateTime.Now.AddYears(-100);
            dateIncident.DisplayDateEnd = DateTime.Now;
            dateIncident.DisplayDateStart = DateTime.Now.AddYears(-10);
            //TexBox Restrictions
            tbCompPhone.DeactivateBlankValidation();
            tbCompPhone.AddValitationRule(new PhoneValidationRule());
            tbVictimPhone.DeactivateBlankValidation();
            tbVictimPhone.AddValitationRule(new PhoneValidationRule());
            tbAggressorPhone.DeactivateBlankValidation();
            tbAggressorPhone.AddValitationRule(new PhoneValidationRule());
            tbCompZone.DeactivateBlankValidation();
            tbCompName.AddValitationRule(new LenghtValidationRule(3));
            tbCompLastName.AddValitationRule(new LenghtValidationRule(3));
            tbCompMiddleName.AddValitationRule(new LenghtValidationRule(3));
            tbCompCI.AddValitationRule(new LenghtValidationRule(6));
            tbCompAddress.AddValitationRule(new LenghtValidationRule(8));
            tbAggressorName.AddValitationRule(new LenghtValidationRule(3));
            tbAggressorLastName.AddValitationRule(new LenghtValidationRule(3));
            tbAggressorMiddleName.AddValitationRule(new LenghtValidationRule(3));
            tbAggressorCi.AddValitationRule(new LenghtValidationRule(6));
            tbAggressorBornPlace.AddValitationRule(new LenghtValidationRule(4));
            tbVictimName.AddValitationRule(new LenghtValidationRule(3));
            tbVictimLastName.AddValitationRule(new LenghtValidationRule(3));
            tbVictimMiddleName.AddValitationRule(new LenghtValidationRule(3));
            tbVictimCI.AddValitationRule(new LenghtValidationRule(6));
            tbVictimBornPlace.AddValitationRule(new LenghtValidationRule(4));
            tbDescription.DeactivateBlankValidation();
        }

        private List<string> GetCountryList()
        {
            List<string> cultureList = new List<string>();
            CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
            foreach (CultureInfo culture in cultures)
            {
                if (!culture.IsNeutralCulture && culture.LCID != 0x7F)
                {
                    RegionInfo region = new RegionInfo(culture.LCID);
                    if (!(cultureList.Contains(region.DisplayName)))
                        cultureList.Add(region.DisplayName);
                }
            }
            var sortedList = (from culture in cultureList
                              orderby culture
                              select culture).ToList();
            return sortedList;
        }

        public void UpdateFirstPartComplaint(string incidentDetail)
        {
            Complaint newComplaint = GatherComlpaintInfo();
            newComplaint.Id = ComplaintId;
            newComplaint.IncidentDetail = incidentDetail;
            newComplaint = ComplaintDataAccess.UpdateComplaint(ComplaintId, newComplaint);
            GetComplainantActorInfo();
            GetAggressorActorInfo();
            GetVictimActorInfo();
            var violenceCauses = GatherViolenceCauses(newComplaint.Id);
            ComplaintDataAccess.UpdateViolenceCauses(ComplaintId, violenceCauses);
            var violenceForms = GatherViolenceForms(newComplaint.Id);
            ComplaintDataAccess.UpdateViolenceForms(ComplaintId, violenceForms);
            var multidisTeam = GatherMultidisciplinaryTeam(newComplaint.Id);
            ComplaintDataAccess.UpdateMultidisciplinaryTeams(ComplaintId, multidisTeam);
            var caseDerivations = GatherCaseDerivations(newComplaint.Id);
            ComplaintDataAccess.UpdateCaseDerivations(ComplaintId, caseDerivations);
        }

        private void SaveAndContinue_Click(object sender, RoutedEventArgs e)
        {
            bool hasErrors = CheckHasErrors();
            if (!hasErrors)
            {
                if (modComplaintpart2 == null)
                    modComplaintpart2 = new ModifyComplaintPart2(this);
                var par = this.Parent as ItemsControl;
                par.Items.Clear();
                var scrollView = (par.Parent as ItemsControl).Parent as ScrollViewer;
                scrollView.ScrollToTop();
                par.Items.Add(modComplaintpart2);
            }
            else
            {
                Xceed.Wpf.Toolkit.MessageBox.Show("Se deben llenar todos los campos indicados para poder continuar!", "Error!");
            }
            
        }

        private bool CheckHasErrors()
        {
            bool res = false;
            foreach (var control in Content.Children)
            {
                GroupBox gp = control as GroupBox;
                if (gp != null)
                {
                    foreach (var ctrl in (gp.Content as Grid).Children)
                    {
                        TextBox tb = ctrl as TextBox;
                        if (tb != null)
                        {
                            tb.GetBindingExpression(TextBox.TextProperty).UpdateSource();
                            if (Validation.GetHasError(tb))
                                res = true;
                        }
                        ValidationComboBox cb = ctrl as ValidationComboBox;
                        if (cb != null)
                        {
                            cb.LostFocusEventHandler(cb,null);
                            if (Validation.GetHasError(cb))
                                res = true;
                        }
                        ValidDatePicker dtp = ctrl as ValidDatePicker;
                        if (dtp != null)
                        {
                            dtp.GetBindingExpression(DatePicker.TextProperty).UpdateSource();
                            dtp.LostFocusEventHandler(dtp, null);
                            if (Validation.GetHasError(dtp))
                                res = true;
                        }
                    }
                }
            }
            bool someCheck = false;
            foreach (var ctrl in ViolenceCause.Children)
            {
                CheckBox chckBox = ctrl as CheckBox;
                
                if (chckBox != null)
                    if (chckBox.IsChecked == true)
                        someCheck = true;
            }
            if (!someCheck)
            {
                lblViolenceCauseError.Content = "Debe seleccionar al menos una causa.";
                res = true;
            }
            if (!(rbtnPrivateAmbit.IsChecked == true || rbtnPublicAmbit.IsChecked == true))
            {
                lblIncidentPlaceError.Content = "Debe seleccionar alguno ambito.";
                res = true;
            }
            if (!(rbtnZoneCentral.IsChecked == true || rbtnZonePeriferic.IsChecked == true || rbtnZoneRural.IsChecked == true))
            {
                lblIncidentZoneError.Content = "Debe seleccionar alguna zona del hecho.";
                res = true;
            }
            someCheck = false;
            foreach (var ctrl in ViolenceForm.Children)
            {
                if (ctrl is CheckBox)
                    if ((ctrl as CheckBox).IsChecked == true)
                        someCheck = true;
            }
            if (!someCheck)
            {
                lblViolenceFormError.Content = "Debe seleccionar al menos una forma de violencia.";
                res = true;
            }
            if (!(chkArrest.IsChecked == true || chkMeeting.IsChecked == true))
            {
                lblCasesAtentionError.Content = "Debe seleccionar al menos una forma de atencion del caso.";
                res = true;
            }
            return res;
        }

        private Complaint GatherComlpaintInfo()
        {
            Complaint complaint = ComplaintDataAccess.GetComplaintById(ComplaintId);
            complaint.IncidentDate = dateIncident.SelectedDate;
            complaint.IncidentPlace = rbtnPrivateAmbit.IsChecked == true
                                ? "Ambito Privado"
                                : rbtnPublicAmbit.IsChecked == true ? "Ambito Publico" : null;
            complaint.IncidentZone = rbtnZoneCentral.IsChecked == true
                               ? "Urbano Central"
                               : (rbtnZoneRural.IsChecked == true
                                      ? "Rural"
                                      : rbtnZonePeriferic.IsChecked == true ? "Urbano Periferica" : null);
            complaint.CasesAttention = chkMeeting.IsChecked == true
                                 ? (chkArrest.IsChecked == true ? "Citacion Y Arresto" : "Citacion")
                                 : (chkArrest.IsChecked == true ? "Arresto" : null);
            complaint.ConductOfAgressor = rbtnAggressorTempSane.IsChecked == true
                                    ? "Sano"
                                    : (rbtnAggressorTempDrunk.IsChecked == true
                                           ? "Ebriedad"
                                           : rbtnAggressorTempDrugs.IsChecked == true ? "Drogas" : null);
            complaint.VictimAgressorRelationship = (cboxVictimPartnership.SelectedItem as ComboBoxItem).Content.ToString();
            complaint.ComplainantVictimRelationship = (cboxCompPartnership.SelectedItem as ComboBoxItem).Content.ToString();
            complaint.RegistrationDate = DateTime.Now;
            complaint.Status = false;
            return complaint;
        }

        private List<ViolenceCause> GatherViolenceCauses(long idComplaint)
        {
            List<ViolenceCause> violenceCauses = new List<ViolenceCause>();

            if (chkEconomics.IsChecked == true)
                violenceCauses.Add(new ViolenceCause { IdComplaint = idComplaint, Cause = chkEconomics.Content.ToString(), Status = false });
            if (chkFamiliar.IsChecked == true)
                violenceCauses.Add(new ViolenceCause { IdComplaint = idComplaint, Cause = chkFamiliar.Content.ToString(), Status = false });
            if (chkJealous.IsChecked == true)
                violenceCauses.Add(new ViolenceCause { IdComplaint = idComplaint, Cause = chkJealous.Content.ToString(), Status = false });
            if (chkInfidelity.IsChecked == true)
                violenceCauses.Add(new ViolenceCause { IdComplaint = idComplaint, Cause = chkInfidelity.Content.ToString(), Status = false });
            if (chkOthersNature.IsChecked == true)
                violenceCauses.Add(new ViolenceCause { IdComplaint = idComplaint, Cause = chkOthersNature.Content.ToString(), Status = false });
            if (chkDomainHabits.IsChecked == true)
                violenceCauses.Add(new ViolenceCause { IdComplaint = idComplaint, Cause = chkDomainHabits.Content.ToString(), Status = false });
            return violenceCauses;
        }

        private List<ViolenceForm> GatherViolenceForms(long idComplaint)
        {
            List<ViolenceForm> violenceForms = new List<ViolenceForm>();

            if (chkPhysics.IsChecked == true)
                violenceForms.Add(new ViolenceForm { IdComplaint = idComplaint, Form = chkPhysics.Content.ToString(), Description = tbDescription.Text, Status = false });
            if (chkPsicologics.IsChecked == true)
                violenceForms.Add(new ViolenceForm { IdComplaint = idComplaint, Form = chkPsicologics.Content.ToString(), Description = tbDescription.Text, Status = false });
            if (chkSexual.IsChecked == true)
                violenceForms.Add(new ViolenceForm { IdComplaint = idComplaint, Form = chkSexual.Content.ToString(), Description = tbDescription.Text, Status = false });
            if (chkOthersViolenceForm.IsChecked == true)
                violenceForms.Add(new ViolenceForm { IdComplaint = idComplaint, Form = chkOthersViolenceForm.Content.ToString(), Description = tbDescription.Text, Status = false });

            return violenceForms;
        }

        private List<MultidisciplinaryTeam> GatherMultidisciplinaryTeam(long idComplaint)
        {
            List<MultidisciplinaryTeam> multidisciplinaryTeam = new List<MultidisciplinaryTeam>();
            if (chkMedic.IsChecked == true)
                multidisciplinaryTeam.Add(new MultidisciplinaryTeam { IdComplaint = idComplaint, Professional = chkMedic.Content.ToString(), Status = false });
            if (chkPsicology.IsChecked == true)
                multidisciplinaryTeam.Add(new MultidisciplinaryTeam { IdComplaint = idComplaint, Professional = chkPsicology.Content.ToString(), Status = false });
            if (chkSocialWorker.IsChecked == true)
                multidisciplinaryTeam.Add(new MultidisciplinaryTeam { IdComplaint = idComplaint, Professional = chkSocialWorker.Content.ToString(), Status = false });
            if (chkLegalAsessor.IsChecked == true)
                multidisciplinaryTeam.Add(new MultidisciplinaryTeam { IdComplaint = idComplaint, Professional = chkLegalAsessor.Content.ToString(), Status = false });
            return multidisciplinaryTeam;
        }

        private List<CaseDerivation> GatherCaseDerivations(long idComplaint)
        {
            List<CaseDerivation> caseDerivations = new List<CaseDerivation>();
            if (chkDerivJudge.IsChecked == true)
                caseDerivations.Add(new CaseDerivation { IdComplaint = idComplaint, Institution = chkDerivJudge.Content.ToString(), Status = false });
            if (chkDerivPublicMinistery.IsChecked == true)
                caseDerivations.Add(new CaseDerivation { IdComplaint = idComplaint, Institution = chkDerivPublicMinistery.Content.ToString(), Status = false });
            if (chkDerivFELCC.IsChecked == true)
                caseDerivations.Add(new CaseDerivation { IdComplaint = idComplaint, Institution = chkDerivFELCC.Content.ToString(), Status = false });
            if (chkDerivDefensory.IsChecked == true)
                caseDerivations.Add(new CaseDerivation { IdComplaint = idComplaint, Institution = chkDerivDefensory.Content.ToString(), Status = false });
            if (chkDerivOther.IsChecked == true)
                caseDerivations.Add(new CaseDerivation { IdComplaint = idComplaint, Institution = chkDerivOther.Content.ToString(), Status = false });
            return caseDerivations;
        }

        private void GetComplainantActorInfo()
        {
            Actor complainant = PersonAndActorDataAccess.GetActorByTypeAndComplaintId("Denunciante", ComplaintId);
            var personComplainant = complainant.Person;

            personComplainant.Id = tbCompCI.Text + personComplainant.RegisterDate.Value.ToShortDateString();
            personComplainant.CiPassport = long.Parse(tbCompCI.Text);
            personComplainant.Name = tbCompName.Text;
            personComplainant.PaternalName = tbCompLastName.Text;
            personComplainant.MaternalName = tbCompMiddleName.Text;
            personComplainant.BirthDate = CompBornDate.DisplayDate;
            personComplainant.Gender = rbtnMaleComp.IsChecked == true ? "M" : "F";
            personComplainant.Phone = tbCompPhone.Text;
            personComplainant.Address = tbCompAddress.Text;
            personComplainant.Zone = tbCompZone.Text;
            personComplainant.Nationality =
                cboxCompCountry.SelectedItem.ToString();
            personComplainant.Ocupation = tbOcc.Text;
            personComplainant.CivilStatus =
                (cbCompCivilStatus.SelectedItem as ComboBoxItem).Content.ToString();
            PersonAndActorDataAccess.UpdatePerson(personComplainant.CiPassport, personComplainant);
        }

        private void GetAggressorActorInfo()
        {
            Actor atorAggressor = PersonAndActorDataAccess.GetActorByTypeAndComplaintId("Agresor", ComplaintId);
            var aggressor = atorAggressor.Person;
            aggressor.Id = tbAggressorCi.Text + aggressor.RegisterDate.Value.ToShortDateString();
            aggressor.CiPassport = long.Parse(tbAggressorCi.Text);
            aggressor.Name = tbAggressorName.Text;
            aggressor.PaternalName = tbAggressorLastName.Text;
            aggressor.MaternalName = tbAggressorMiddleName.Text;
            aggressor.BirthDate = AggressorBornDate.DisplayDate;
            aggressor.Gender = rbtnMaleAggressor.IsChecked == true ? "M" : "F";
            aggressor.Phone = tbAggressorPhone.Text;
            aggressor.CivilStatus =
                (cbAggressorCivilStatus.SelectedItem as ComboBoxItem).Content.ToString();
            aggressor.Nationality =
                cbAggressorCountry.SelectedItem.ToString();
            aggressor.BornCity = tbAggressorBornPlace.Text;
            aggressor.BornArea = rbtnAggressorRural.IsChecked == true
                                             ? "Urbana"
                                             : rbtnAggressorUrban.IsChecked == true ? "Rural" : null;
            aggressor.LevelOfEducation =
                (cbAggressorInstructionLevel.SelectedItem as ComboBoxItem).Content.ToString();
            aggressor.ActivityStatus = rbtnAggressorCondActBusy.IsChecked == true
                                                   ? rbtnAggressorCondActBusy.Content.ToString()
                                                   : (rbtnAggressorCondActFree.IsChecked == true
                                                          ? rbtnAggressorCondActFree.Content.ToString()
                                                          : rbtnAggressorCondActInactive.IsChecked == true
                                                                ? rbtnAggressorCondActInactive.Content.ToString()
                                                                : null);
            PersonAndActorDataAccess.UpdatePerson(aggressor.CiPassport, aggressor);
        }

        private void GetVictimActorInfo()
        {
            Actor actorVictim = PersonAndActorDataAccess.GetActorByTypeAndComplaintId("Victima", ComplaintId);
            Person victim = actorVictim.Person;
            victim.Id = tbVictimCI.Text + victim.RegisterDate.Value.ToShortDateString();
            victim.CiPassport = long.Parse(tbVictimCI.Text);
            victim.Name = tbVictimName.Text;
            victim.PaternalName = tbVictimLastName.Text;
            victim.MaternalName = tbVictimMiddleName.Text;
            victim.BirthDate = VictimBornDate.DisplayDate;
            victim.Gender = rbtnMaleVictim.IsChecked == true ? "M" : "F";
            victim.Phone = tbVictimPhone.Text;
            victim.CivilStatus =
                (cbVictimCivilStatus.SelectedItem as ComboBoxItem).Content.ToString();
            victim.Nationality =
                cbVictimCountry.SelectedItem.ToString();
            victim.BornCity = tbVictimBornPlace.Text;
            victim.BornArea = rbtnVictimUrban.IsChecked == true
                                  ? "Urbana"
                                  : rbtnVictimRural.IsChecked == true ? "Rural" : null;
            victim.LevelOfEducation = (cbVictimInstructionLevel.SelectedItem as ComboBoxItem).Content.ToString();
            victim.ActivityStatus = rbtnVictimCondActBusy.IsChecked == true
                                        ? rbtnVictimCondActBusy.Content.ToString()
                                        : (rbtnVictimCondActFree.IsChecked == true
                                               ? rbtnVictimCondActFree.Content.ToString()
                                               : rbtnVictimCondActInactive.IsChecked == true
                                                     ? rbtnVictimCondActInactive.Content.ToString()
                                                     : null);
            PersonAndActorDataAccess.UpdatePerson(victim.CiPassport,victim);
        
        }

        private void ViolenceCauseChecked(object sender, RoutedEventArgs e)
        {
            lblViolenceCauseError.Content = "";
        }

        private void IncidentPlaceChecked(object sender, RoutedEventArgs e)
        {
            lblIncidentPlaceError.Content = "";
        }

        private void IncidentZoneChecked(object sender, RoutedEventArgs e)
        {
            lblIncidentZoneError.Content = "";
        }

        private void ViolenceFormChecked(object sender, RoutedEventArgs e)
        {
            lblViolenceFormError.Content = "";
        }

        private void CasesAtentionChecked(object sender, RoutedEventArgs e)
        {
            lblCasesAtentionError.Content = "";
        }

        private void BtnKeyCompCi_OnClick(object sender, RoutedEventArgs e)
        {
            var grantPermission = new GrantDirectorPermissionWindows();
            if (grantPermission.ShowDialog() == true)
            {
                tbCompCI.IsEnabled = true;
            }
        }

        private void BtnKeyAggressorCiClick(object sender, RoutedEventArgs e)
        {
            var grantPermission = new GrantDirectorPermissionWindows();
            if (grantPermission.ShowDialog() == true)
            {
                tbAggressorCi.IsEnabled = true;
            }
        }

        private void BtnKeyVictimCi_OnClick(object sender, RoutedEventArgs e)
        {
            var grantPermission = new GrantDirectorPermissionWindows();
            if (grantPermission.ShowDialog() == true)
            {
                tbVictimCI.IsEnabled = true;
            }
        }
    }
}
